package com.dk.controller;

import com.dk.em.Result;
import com.dk.page.PageResult;
import com.dk.pojo.User;
import com.dk.service.Userservice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("user")
@Api(tags = "用户服务")
@RefreshScope     //开启热更新
public class UserController {
    
    @Autowired
    private Userservice userservice;

    /*增加ok*/
    @PostMapping
    @ApiOperation("用户增加接口")
    @ApiImplicitParam(name = "用户对象信息",value = "user",required = false)
    public Result insertuser(@RequestBody User user){
        userservice.insertuser(user);
        return Result.OK();
    }


    /*根据id删除ok*/
    @DeleteMapping("deleteById")
    @ApiOperation("根据id删除")
    public Result deleteuser(Integer id){
        userservice.deleteById(id);
        return Result.OK();
    }


    //批量删除ok
    @DeleteMapping
    @ApiOperation("根据id批量删除")
    public Result deleteAlluser(@RequestBody List<String> ids){
        userservice.deleteAllById(ids);
        return Result.OK();
    }


    /*查询ok*/
    @GetMapping
    @ApiOperation("查询+分页")
    @ApiImplicitParams({@ApiImplicitParam(value = "pageNum", name = "当前页数", required = true),
            @ApiImplicitParam(value = "pageSize", name = "每页条数", required = false)})
    public Result getAlluser(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        PageResult<User> pageInfo = userservice.getAlluser(pageNum,pageSize);
        return Result.OK().setData(pageInfo);
    }

    //模糊查询ok
    @ApiOperation("根据用户名称模糊查询")
    @ApiImplicitParam(name = "用户名称",value = "name",required = true)//必填
    @GetMapping("getuserByName")
    public Result getuserByName(String name){
        List<User> userList = userservice.getuserByName(name);
        return Result.OK().setData(userList);
    }


    //回显根据id查询数据ok
    @ApiImplicitParam(name = "用户id",value = "id",required = true)
    @GetMapping("getById")
    public Result getById(Integer id){
        User user = userservice.getById(id);
        return Result.OK().setData(user);
    }


    /*修改*/
    @PutMapping
    @ApiOperation("修改用户接口")
    @ApiImplicitParam(name = "用户对象",value = "user",required = false)
    public Result updateuser(@RequestBody User user){
        userservice.updateuser(user);
        return Result.OK();
    }

    /*根据名称查询User*/
    @PostMapping("LogingetUserByName")
    public Result<User> LogingetUserByName(String username, HttpServletRequest request){
        log.info("获取到负载均衡的端口号:"+request.getServerPort());
        User user = userservice.LogingetuserByName(username);
        return Result.OK().setData(user);
    }
}
